
package com.zhongshan.ga.mobile.logic;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.SqlSession;
import org.restlet.representation.Representation;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zhongshan.ga.commonvar.ArgsConfig;
import com.zhongshan.ga.commonvar.CommendConfig;
import com.zhongshan.ga.logger.GenerateLog;
import com.zhongshan.ga.mybatis.dao.ITravelMateInfoDao;
import com.zhongshan.ga.utils.MyBatisUtil;

/**
 * 获得结伴游详情的逻辑处理类
 * @author yanqi
 *
 */
public class GetTravelMateDetailLogic extends ParentLogic {
	/**
	 * 获得结伴游详情的逻辑处理方法
	 * @param representation
	 * @return
	 */
	public JsonObject getTravelMateDetail(Representation representation) {
		JsonObject acceptJsonObject;
		JsonObject resultJson = null ;
		Gson gson = new Gson();
		
		try {
			/**
			 * 获取客户客户端请求的内容
			 */
			String acceptString = representation.getText();
			GenerateLog.outDebug(acceptString);
			acceptJsonObject = new JsonParser().parse(acceptString).getAsJsonObject();
			GenerateLog.outDebug(acceptJsonObject);
			
			/**
			 * 进行基本的逻辑较验
			 */
			resultJson = super.basicCheck(acceptJsonObject, CommendConfig.TRAVEL_MATE_INFO_DETAIL, false);
			
			/**
			 * 判断是否逻辑较验正确
			 */
			if(resultJson.get(ArgsConfig.RESULT_CODE).getAsString().equals(ArgsConfig.RESPONSE_SUCCESS)) {
				GenerateLog.outDebug("进来了");
				GenerateLog.outDebug(acceptJsonObject);
				// 获得请求的数据体
				 JsonObject dataJsonObject = acceptJsonObject.get(ArgsConfig.REQUEST_DATA).getAsJsonObject();
				GenerateLog.outDebug(dataJsonObject);
				
				/**
				 * 获得结伴游编码
				 */
				String travelMateCode = dataJsonObject.get(ArgsConfig.TRAVEL_MATE_CODE).getAsString();
				
				// 获得sqlSession对象
				SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
				// 获得查询对象
				ITravelMateInfoDao travelMateInfoDao = sqlSession.getMapper(ITravelMateInfoDao.class);
				
				/**
				 * 获得数据
				 */
				// 获得详情
				Object travelMateInfoList = sqlSession.selectOne("com.mybatis.mapping.travelMateMapper.selectTravelMateDetaiByTravelMateCode", travelMateCode);
				// 获取评论列表
				List<Object> travelMateCommentList = sqlSession.selectList("com.mybatis.mapping.travelMateMapper.selectCommentList", travelMateCode);
				// 获得加入头像
				ArrayList<String> userHeads = travelMateInfoDao.selectJoinPeopleImg(travelMateCode);
				// 获得评论数
				int commentNum = travelMateInfoDao.selectTravelCommentCount(travelMateCode);
				// 获得参加人数
				int peopleNum = travelMateInfoDao.selectTravelCommentCount(travelMateCode);
				
				JsonParser jsonParser = new JsonParser();
				GenerateLog.outDebug(gson.toJson(travelMateInfoList));
				GenerateLog.outDebug(gson.toJson(travelMateCommentList));
				GenerateLog.outDebug(gson.toJson(userHeads));
				GenerateLog.outDebug(gson.toJson(commentNum));
				GenerateLog.outDebug(gson.toJson(peopleNum));
				GenerateLog.outDebug(gson.toJson(travelMateInfoList));
				JsonObject jsonObejct = jsonParser.parse(gson.toJson(travelMateInfoList)).getAsJsonObject();
				GenerateLog.outDebug("****************************结果数据为："+ jsonObejct +"**********************");
				JsonObject tempJsonObject = acceptJsonObject.get(ArgsConfig.REQUEST_DATA).getAsJsonObject();
				GenerateLog.outDebug(tempJsonObject);
				// 加入评论数
				jsonObejct.addProperty(ArgsConfig.COMMENT_NUM, commentNum);
				GenerateLog.outDebug("****************************结果数据为："+ jsonObejct +"**********************");
				// 加入参加的人数
				jsonObejct.addProperty(ArgsConfig.JOIN_PEOPLE_NUM, peopleNum);
				GenerateLog.outDebug("****************************结果数据为："+ jsonObejct +"**********************");
				
				// 将参加的用户头像转为JsonArray
				JsonArray userHeadArray = jsonParser.parse(gson.toJson(userHeads)).getAsJsonArray();
				jsonObejct.add("userHeads", userHeadArray);
				
				// 将评论列表转为jsonArray
				JsonArray commentArray = jsonParser.parse(gson.toJson(travelMateCommentList)).getAsJsonArray();
				for (int i = 0; i < commentArray.size(); i++) {
					JsonObject commentJsonObject = commentArray.get(i).getAsJsonObject();
					String replytoPeople = commentJsonObject.get("replyToPeople").getAsString();
					if(replytoPeople.equals("0") || replytoPeople == "0") {
						// 加入评论内容的则回复者为0
						commentJsonObject.addProperty("replyToPeopleNickName","0");
					} else {
						// 获得被回复者的
						String nickName = travelMateInfoDao.selectNickNameByPeopleSystemCode(replytoPeople);
						// 加入被回复者的昵称
						commentJsonObject.addProperty("replyToPeopleNickName", nickName);
					}
					GenerateLog.outDebug("JsonObject" + commentJsonObject+"--->" + i);
					commentArray.set(i, commentJsonObject);
					GenerateLog.outDebug("JsonArray"+commentArray+"--->" + i);
				}
				// 将评论列表加入列表
				jsonObejct.add("travelMateCommentBeans", commentArray);
				
				tempJsonObject.add(ArgsConfig.RESULT_DATA, jsonObejct);
				GenerateLog.outDebug(tempJsonObject+"11111111111111111");
				
				resultJson.add(ArgsConfig.REQUEST_DATA, acceptJsonObject.get(ArgsConfig.REQUEST_DATA).getAsJsonObject());
				GenerateLog.outDebug("***************************返回的数据：" + resultJson + "**********************************");
				
			}
			
			return resultJson;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获得评论列表的逻辑处理类
	 * @param representation
	 * @return
	 */
	public JsonObject getTravelMateCommentList(Representation representation) {
		JsonObject acceptJsonObject;
		JsonObject resultJson = null ;
		Gson gson = new Gson();
		
		try {
			/**
			 * 获取客户客户端请求的内容
			 */
			String acceptString = representation.getText();
			GenerateLog.outDebug(acceptString);
			acceptJsonObject = new JsonParser().parse(acceptString).getAsJsonObject();
			GenerateLog.outDebug(acceptJsonObject);
			
			/**
			 * 进行基本的逻辑较验
			 */
			resultJson = super.basicCheck(acceptJsonObject, CommendConfig.TRAVEL_MATE_INFO_DETAL_COMMENT, false);
			
			/**
			 * 判断是否逻辑较验正确
			 */
			if(resultJson.get(ArgsConfig.RESULT_CODE).getAsString().equals(ArgsConfig.RESPONSE_SUCCESS)) {
				GenerateLog.outDebug("进来了");
				GenerateLog.outDebug(acceptJsonObject);
				// 获得请求的数据体
				 JsonObject dataJsonObject = acceptJsonObject.get(ArgsConfig.REQUEST_DATA).getAsJsonObject();
				GenerateLog.outDebug(dataJsonObject);
				
				/**
				 * 获得结伴游编码
				 */
				String travelMateCode = dataJsonObject.get(ArgsConfig.TRAVEL_MATE_CODE).getAsString();
				GenerateLog.outDebug(travelMateCode + "###############################");
				// 获得请求的记录下标
				int startIndex = dataJsonObject.get(ArgsConfig.START_INDEX).getAsInt();
				int endIndex = dataJsonObject.get(ArgsConfig.RECORD_NUM).getAsInt();
				
				// 设置条件参数
				Map<String, Object> paramMap = new HashedMap();
				paramMap.put("startIndex", startIndex);
				paramMap.put("endIndex", endIndex);
				paramMap.put("travelMateCode", travelMateCode);
				
				// 获得sqlSession对象
				SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
				// 获得查询对象
				ITravelMateInfoDao travelMateInfoDao = sqlSession.getMapper(ITravelMateInfoDao.class);
				
				// 获取评论列表
				List<Object> travelMateCommentList = sqlSession.selectList("com.mybatis.mapping.travelMateMapper.selectCommentComments", paramMap);
				GenerateLog.outDebug(gson.toJson(travelMateCommentList));
				/**
				 * 获得每次查询的记录总数
				 */
				String reObject = sqlSession.selectOne("com.mybatis.mapping.travelMateMapper.selectCommentListCountByLimit", paramMap);
				if(reObject == null) {
					reObject = "0";
				}
				GenerateLog.outDebug(reObject.toString());
				
				JsonParser jsonParser = new JsonParser();
				JsonArray jsonArray = jsonParser.parse(gson.toJson(travelMateCommentList)).getAsJsonArray();
				GenerateLog.outDebug("****************************结果数据为："+ jsonArray +"**********************");
				
				for (int i = 0; i < jsonArray.size(); i++) {
					JsonObject commentJsonObject = jsonArray.get(i).getAsJsonObject();
					String replytoPeople = commentJsonObject.get("replyToPeople").getAsString();
					if(replytoPeople.equals("0") || replytoPeople == "0") {
						// 加入评论内容的则回复者为0
						commentJsonObject.addProperty("replyToPeopleNickName","0");
					} else {
						// 获得被回复者的
						String nickName = travelMateInfoDao.selectNickNameByPeopleSystemCode(replytoPeople);
						// 加入被回复者的昵称
						commentJsonObject.addProperty("replyToPeopleNickName", nickName);
					}
					GenerateLog.outDebug("JsonObject" + commentJsonObject+"--->" + i);
					jsonArray.set(i, commentJsonObject);
					GenerateLog.outDebug("JsonArray"+jsonArray+"--->" + i);
				}
				
				JsonObject tempJsonObject = acceptJsonObject.get(ArgsConfig.REQUEST_DATA).getAsJsonObject();
				GenerateLog.outDebug(tempJsonObject);
				tempJsonObject.add(ArgsConfig.RESULT_DATA, jsonArray);
				GenerateLog.outDebug(tempJsonObject+"11111111111111111");;
				
				resultJson.add(ArgsConfig.REQUEST_DATA, acceptJsonObject.get(ArgsConfig.REQUEST_DATA).getAsJsonObject());
				resultJson.addProperty(ArgsConfig.TOTAL_RECORD_NUM_EVERY_TIME, Integer.valueOf(reObject));
				GenerateLog.outDebug("***************************返回的评论数据：" + resultJson + "**********************************");
			}
			return resultJson;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
}
